﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using NationalInstruments.VisaNS;
using System.Runtime.InteropServices;

namespace chromemeter
{
    class ClassChromeMeter
    {
        [DllImport("Kmsecs200.dll")]
        public static extern int int_usb(int Index);   
        [DllImport("Kmsecs200.dll")]
        public static extern int end_usb(int Index);   
        [DllImport("Kmsecs200.dll")]
        public static extern int get_num();
        [DllImport("Kmsecs200.dll")]
        public static extern int write64_usb(int Index, string Cmd, int Timeout, int WriteLen);   
        [DllImport("Kmsecs200.dll")]
        public static extern int read64_usb(int Index, StringBuilder Dat, int Timeout, int ReadLen);   


        int Index = 0;
        string cRemote = "RMT,1\r\n";
        StringBuilder chr = new StringBuilder(1024);
        string cMes = "MES,1\r\n";
        StringBuilder chr1 = new StringBuilder(1024);
        string cMdr = "MDR,0\r\n";
        StringBuilder chr2 = new StringBuilder(1024);

        public int join()
        {
            int ret = 1;//-------------------------------连接成功标志位

            try
            {
               ret=int_usb(Index);
            }
            catch {  }
            return ret;

        }
        public void RMT()
        {
            int ret = 88;
            int res = 88;

            string rev;

            ret = write64_usb(Index, cRemote, 1, 7);
            res = read64_usb(0, chr, 1, 250);
            rev = "" + chr;


            //return rev;
        }

        public string MES()
        {
            int ret = 1;
            int res = 1;
            string rev;
            ret = write64_usb(Index, cMes, 1, 7);
            res = read64_usb(0, chr1, 1, 250);
            rev = "" + chr1;
            return rev;
        }
        public string MDR()
        {
            int ret = 1;
            int res = 1;
            string rev;
            ret = write64_usb(Index, cMdr, 1, 7);
            res = read64_usb(0, chr2, 1, 250);
            rev = "" + chr2;
            return rev;
        }


        public int exit()
        {
            int ret = 1;
            ret = end_usb(Index);
            return ret;
        }
    }
}
